<?php

namespace app\common\controller;

use think\Controller;
use external\SFTools;
use think\facade\Log;

class ServiceOauth extends Controller {

    protected $appFind;

    protected $userFind;

    protected function initialize() {
        $this->host_check("json");
        $this->doc_check();
        //$this->post_check("json");
        $this->pvt_oautCheck();
    }
    
    protected function doc_check() {
        if (request()->isGet()) {
            header("Content-type: text/html; charset=utf-8");
            $module = request()->module();
            $controller = request()->controller();
            $action = request()->action();
            $class = 'app\\' . $module . '\\controller\\' . $controller;
            $titles = array();
            $params = array();
            $returns = array();
            $method = new \ReflectionMethod($class, $action);
            $method_doc = $method->getDocComment ();
            $method_doc = str_replace("\r\n", "", trim($method_doc));
            $method_doc = str_replace("/**", "", trim($method_doc));
            $method_doc = str_replace("*/", "", trim($method_doc));
            $method_doc_arr = explode('*', trim($method_doc));
            foreach ($method_doc_arr as $key => $value) {
                if(!empty($value) && trim($value) !== '/**' && trim($value) !== '*/'){
                    if(strpos($value, '@return') !== false){
                        $returnStrArr = explode('·', str_replace("@return", "·", $value));
                        if(!empty(trim($returnStrArr[1]))) {
                            $explodeReturnArr = explode(' ', trim($returnStrArr[1]));
                            $returnArr = array();
                            foreach ($explodeReturnArr as $index => $val) {
                                if(!empty($val)){
                                    if(count($returnArr) == 2){
                                        $returnArr[1] = $returnArr[1] . $val;
                                    }else{
                                        $returnArr[] = $val;
                                    }
                                }
                            }
                            $returns[] = array(
                                'type'=> $returnArr[0],
                                'desc'=> $returnArr[1],
                            );
                        }
                    }elseif (strpos($value, '@param') !== false){
                        $paramStrArr = explode('·', str_replace("@param", "·", $value));
                        if(!empty(trim($paramStrArr[1]))) {
                            $explodeParamArr = explode(' ', trim($paramStrArr[1]));
                            $paramArr = array();
                            foreach ($explodeParamArr as $index => $val) {
                                if(!empty($val)){
                                    if(count($paramArr) == 3){
                                        $paramArr[2] = $paramArr[2] . $val;
                                    }else{
                                        $paramArr[] = $val;
                                    }
                                }
                            }
                            $params[] = array(
                                'type'=> $paramArr[0],
                                'name'=> $paramArr[1],
                                'desc'=> $paramArr[2],
                            );
                        }
                    }else{
                        $titleStrArr = explode(' ', $value);
                        if(!empty(trim($titleStrArr[1]))) {
                            $titles[] = trim($titleStrArr[1]);
                        }
                    }
                    
                }
            }
            echo '<html><head><title>接口文档</title><link href="/static/plugins/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /></head><body>';
            echo '<div style="height:50px;"></div><table class="table table-bordered">';
            echo '<thead>';
            foreach ($titles as $key => $value) {
                echo '<tr><th colspan="3">' . $value . '</th></tr>';
            }
            echo '</thead><tbody><tr class="success"><th colspan="3">传递参数</th></tr>';
            echo '<tr class="active"><th>参数</th><th>类型</th><th>描述</th></tr>';
            foreach ($params as $key => $value) {
                echo '<tr><td>' . $value['name'] . '</td><td>' . $value['type'] . '</td><td>' . $value['desc'] . '</td></tr>';
            }
            echo '</tbody><tfoot>';
            echo '<tr class="danger"><th colspan="3">返回参数</th></tr>';
            echo '<tr class="active"><th>类型</th><th colspan="2">描述</th></tr>';
            foreach ($returns as $key => $value) {
                echo '<tr><td>' . $value['type'] . '</td><td colspan="2">' . $value['desc'] . '</td></tr>';
            }
            echo '</tfoot></body></html>';
            exit;
        }
    }

    protected function get_check($type = "json") {
        if (!request()->isGet()) {
            if ($type == "json") {
                exit(SFTools::pullJson(0, '3002'));
            }
            elseif ($type == "page") {
                $msg = lang("RETURN_CODE_3002");
                $this->assign('code', "3002");
                $this->assign('msg', $msg);
                return SFTools::toErrorPage("3002");
            }
            else {
                exit("抱歉，您没有权限访问！");
            }
        }
    }

    protected function post_check($type = "json") {
        if (!request()->isPost()) {
            if ($type == "json") {
                exit(SFTools::pullJson(0, '3002'));
            }
            elseif ($type == "page") {
                $msg = lang("RETURN_CODE_3002");
                $this->assign('code', "3002");
                $this->assign('msg', $msg);
                return SFTools::toErrorPage("3002");
            }
            else {
                exit("抱歉，您没有权限访问！");
            }
        }
    }

    protected function host_check($type = "json") {
        $host = $_SERVER['HTTP_HOST'];
        if ($host !== SERVICE_BIND_HOST) {
            if ($type == "json") {
                exit(SFTools::pullJson(0, '3002'));
            }
            elseif ($type == "page") {
                $msg = lang("RETURN_CODE_3002");
                $this->assign('code', "3002");
                $this->assign('msg', $msg);
                return SFTools::toErrorPage("3002");
            }
            else {
                exit("抱歉，您没有权限访问！");
            }
        }
    }

    private function pvt_oautCheck() {
        $app_id = htmlspecialchars(input("get.appid"));
        $access_token = htmlspecialchars(input("get.access_token"));
        $t = htmlspecialchars(input("get.t"));
        if (empty($app_id)) {
            exit(SFTools::pullJson(0, '3102'));
        }
        if (empty($access_token)) {
            exit(SFTools::pullJson(0, '3101'));
        }
        $this->appFind = SFTools::getCache($app_id);
        if (empty($this->appFind)) {
            exit(SFTools::pullJson(0, '3202'));
        }
        if ($this->appFind['access_token'] != $access_token) {
            Log::record("appFind['access_token']: {$this->appFind['access_token']} & access_token: {$access_token}" , 'error');
            exit(SFTools::pullJson(0, '3201'));
        }
        if(!empty($t)){
            $this->userFind = SFTools::getCache($t);
        }
    }

}
